< previous page page_507 next page >

Page 507
Type.gifListing 13.3. Adding the PRIMARY KEY CONSTRAINT.
//
// testing SQL Table Management Keywords
//
// create a new database for our tests
dbMake sqlvb02.mdb;
// create a simple table
CREATE TABLE NewTable
    (Field1 TEXT(30) CONSTRAINT PKNewTable PRIMARY KEY,
     Field2 INTEGER);
// show the empty table
SELECT * FROM NewTable;
// eof
Notice that the CREATE TABLE SQL statement is spread out over more than one line of text. SQL statements can stretch over several lines, as long as each complete SQL statement ends with a semicolon. The continued lines need not be indented, but doing so makes it easier to read the SQL scripts.
You look at the CONSTRAINT clause in depth a bit later. For now, remember that you can create both primary and foreign keys in a CREATE TABLE statement.
Modifying Tables with ALTER TABLE_ADD COLUMN and DROP COLUMN
There are two forms of the ALTER TABLE statement: the ADD COLUMN form and the DROP COLUMN form. The ADD COLUMN form enables you to add new columns to an existing table without losing any data in the existing columns. Edit the SQLVB02.SQV script using SQL-VB5 so that it matches the script in Listing 13.4.
Type.gifListing 13.4. Using the ADD COLUMN clause.
//
// testing SQL Table Management Keywords
//
// create a new database for our tests
dbMake sqlvb02.mdb;
// create a simple table
CREATE TABLE NewTable
    (Field1 TEXT(30) CONSTRAINT PKNewTable PRIMARY KEY,
     Field2 INTEGER);
// add a two new columns
ALTER TABLE NewTable ADD COLUMN Field3 DATE;
ALTER TABLE NewTable ADD COLUMN Field4 CURRENCY;
// show the empty table
SELECT * FROM NewTable;
// eof

 
< previous page page_507 next page >

If you like this book, buy it!